﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DSS_Simplex
{
    public partial class FormConst : Form
    {
        private string textFilter;
        private DataSet ds;
        FormConstPop formSearch = null;

        CheckBox chkbox = new CheckBox();
       
        public FormConst()
        {
            InitializeComponent();
            BindDatakeGrid();
        }

        private void BindDatakeGrid() 
        {
            try
            {
                ds = new DataSet();
                FileCmd.BindToDataSet("SELECT nama_const, min_const, max_const FROM `constraint`", ref ds);
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format(ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void chkBoxChange(object sender, EventArgs e)
        {
            for (int k = 0; k <= dataGridView1.RowCount - 1; k++)
            {
                this.dataGridView1[0, k].Value = this.chkbox.Checked;
            }
            this.dataGridView1.EndEdit();
        }

        private void searchgrid_TextChanged(List<field> fields)
        {
            System.Data.DataView dv = ((System.Data.DataView)dataGridView1.DataSource);
            textFilter = ""; //reset

            bool first = true;
            foreach (field f in fields)
            {
                if (f.Value.Length > 0)
                {
                    if (!first) textFilter += " and ";
                    textFilter += f.Field + " like '%" + f.Value + "%'";
                    first = false;
                }
            }
            dv.RowFilter = textFilter;
            this.label1.Text = textFilter;
        }

        private void BtnReset_Click(object sender, EventArgs e)
        {
            formSearch = null;
            textFilter = "";
            this.label1.Text = "Filter: None";
            System.Data.DataView dv = ((System.Data.DataView)dataGridView1.DataSource);
            dv.RowFilter = textFilter;
        }

        private void BtnFilter_Click(object sender, EventArgs e)
        {
            if (formSearch == null)
            {
                List<field> fields = new List<field>();
                field f = new field();
                f.FriendlyName = "Const Name";
                f.Field = "nama_const";
                fields.Add(f);

                formSearch = new FormConstPop(fields);
                formSearch.searchgrid_TextChanged += new SearchContextChangedHandler(searchgrid_TextChanged);

            }
            formSearch.ShowDialog();
        }
        private void BtnAdd_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.ToString() != null)
            {
                AddSelectedDGV(dataGridView1);
            }
        }
        private static DataGridView DGTemp, DGV1;
        public static void temporerDG(DataGridView DGView1)
        {
            DGV1 = DGView1;
        }
        public static void AddSelectedDGV(DataGridView datagridview1)
        {
            try
            {
                List<int> listSelect = new List<int>();
                DGTemp = new DataGridView();
                int DGrow = datagridview1.SelectedRows.Count;
                int DGcol = datagridview1.ColumnCount;
                DGTemp.RowCount = DGrow;
                DGTemp.ColumnCount = DGcol;

                for (int i = 0; i < datagridview1.RowCount; i++)
                {
                    if (datagridview1.Rows[i].Selected == true)
                    {
                        listSelect.Add(i);
                    }
                }
                string[] temprow = new string[datagridview1.ColumnCount];
                for (int i = 0; i < listSelect.Count; i++)
                {
                    for (int j = 0; j < datagridview1.ColumnCount; j++)
                    {
                        DGTemp.Rows[i].Cells[j].Value = datagridview1.Rows[listSelect.ElementAt(i)].Cells[j].Value;
                        temprow[j] = DGTemp.Rows[i].Cells[j].Value + "";
                    }
                    DGV1.Rows.Add(temprow);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error: " + ex.Message);
            }
        }
    }
}
